package tcln.gxtx.client.utils.store;

import java.util.Map.Entry;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.Store;

/**
 * An utility class for a store.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class StoreUtils {

    /**
     * Checks if this model list is valid or not - i.e. all changes in all cells are valid.
     * 
     * @param store
     * @param <M>
     * @return <code>true</code> if all model contain only valid cells.
     */
    public static <M extends ModelData> boolean isValid(Store<M> store) {
        for (M model : store.getModels()) {
            for (Entry<String, Object> entry : model.getProperties().entrySet()) {
                if (!store.getRecord(model).isValid(entry.getKey())) {
                    return false;
                }
            }
        }
        return true;
    }
    
    /**
     * Checks if this model list is dirty - i.e. this list has some changes - or not.
     * 
     * @param store
     * @param <M>
     * @return <code>true</code> if there are some dirty records.
     */
    public static <M extends ModelData> boolean isDirty(Store<M> store) {
        for (M model : store.getModels()) {
            if (store.getRecord(model).isDirty()) {
                return true;
            }
        }
        return false;
    }
}
